import type { App } from 'vue';
import LoginRouter from './modules/login';
import LayoutRouter from './modules/layout';
import { close, start } from '@/utils/nprogress';
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';

export const publicRoutes: Array<RouteRecordRaw> = [
	...LoginRouter,
	...LayoutRouter,
	{
		path: '/login',
		redirect: '/login'
	}
];

const router = createRouter({
	history: createWebHashHistory(),
	routes: publicRoutes
});

// 路由前置后卫
router.beforeEach(() => {
	// 开启进度条
	start();
});
// 路由后置后卫
router.afterEach(() => {
	// 关闭进度条
	close();
});

/* 初始化路由表 */
export function resetRouter() {
	router.getRoutes().forEach((route) => {
		const { name } = route;
		if (name) {
			router.hasRoute(name) && router.removeRoute(name);
		}
	});
}
export default router;
export const setupRouter = (app: App<Element>) => {
	app.use(router);
};
